#include<bits/stdc++.h>

using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

int main() {
	
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);

	int n;
	std::cin >> n;
	std::vector g(n * n + 1, std::vector<int>(n + 1, 0));
	for(int i = 1; i <= n * n; i++) {
		for(int j = 1; j <= n; j++) {
			std::cin >> g[i][j];
		}
	}
	std::vector sum(n * n + 1, std::vector<i64>(n + 1, 0));
	for(int i = 1; i <= n; i++) {
		for(int j = 1 + (i - 1) * n; j <= i * n; j++) {
			for(int k = 1; k <= n; k++) {
				if(j == 1 + (i - 1) * n && k == 1) {
					sum[j][k] = g[j][k];
				}else if(j == 1 + (i - 1) * n) {
					sum[j][k] = sum[j][k - 1] + g[j][k];
				}else if(k == 1) {
					sum[j][k] = sum[j - 1][k] + g[j][k];
				}else {
					sum[j][k] = sum[j - 1][k] + sum[j][k - 1] - sum[j - 1][k - 1] + g[j][k];
				}
			}
		}
	}

	/*for(int i = 1; i <= n * n; i++) {
		for(int j = 1; j <= n; j++) {
			std::cout << sum[i][j] << " ";
		}
		std::cout << "\n";
	}
	std::cout << "\n";*/
	int q;
	std::cin >> q;
	for(int i = 0; i < q; i++) {
		int lx, rx, ly, ry, lz, rz;
		std::cin >> lx >> rx >> ly >> ry >> lz >> rz;
		i64 ans = 0;
		for(int j = lx; j <= rx; j++) {
			int t1 = (j - 1) * n;
			int x1 = t1 + ly, y1 = lz;
			int x2 = t1 + ry, y2 = rz;
			// std::cerr << x1 << " " << y1 << " " << x2 << " " << y2 << "\n";
			if(x1 - 1 == t1 && y1 - 1 == t1) {
				ans += sum[x2][y2];
			}else if(x1 - 1 == t1) {
				ans += sum[x2][y2] - sum[x2][y1 - 1]; 
			}else if(y1 - 1 == t1) {
				ans += sum[x2][y2] - sum[x1 - 1][y2]; 
			}else {
				ans += sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1]; 
			}
			
		}
		std::cout << ans << "\n";
	}

	return 0;
}